package com.lcredu.web;

import com.lcredu.entity.Furn;
import com.lcredu.entity.Page;
import com.lcredu.service.FurnService;
import com.lcredu.service.impl.FurnServiceImpl;
import com.lcredu.utils.DataUtils;
import com.lcredu.utils.DefaultMsg;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author 黎长容
 * @date 2025-07-07 15:20
 */
@WebServlet(name = "CustomerFurnServlet", urlPatterns = "/CustomerFurnServlet")
public class CustomerFurnServlet extends BasicServlet {
    private FurnService furnService = new FurnServiceImpl();
    //转发到首页

    protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum = DataUtils.parserInt(request.getParameter("pageNum"), 1);
        int pageSize = DataUtils.parserInt(request.getParameter("pageSize"), DefaultMsg.DEFAULT_PAGE_SIZE.getPageSize());

        //调用方法
        Page<Furn> page = furnService.getPage(pageNum, pageSize);
        //将page放入域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/views/customer/index.jsp").forward(request, response);

    }

    protected void pageByName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum = DataUtils.parserInt(request.getParameter("pageNum"), 1);
        int pageSize = DataUtils.parserInt(request.getParameter("pageSize"), DefaultMsg.DEFAULT_PAGE_SIZE.getPageSize());
        //如果什么参数都没有,将查找所有数据
        String name = request.getParameter("name");
        if (null == name) {
            name = "";
        }
        //调用方法
        Page<Furn> page = furnService.getPageByName(pageNum, pageSize, name);

        StringBuilder url = new StringBuilder("CustomerFurnServlet?action=pageByName");
        if (!"".equals(name)) {
            url.append("&name=").append(name);
        }
        page.setUrl(url.toString());
        //将page放入域中
        request.setAttribute("page", page);
        request.getRequestDispatcher("/views/customer/index.jsp").forward(request, response);
    }
}
